import {
	getUserInfo,
	saveUserInfo
} from "@/api/members";
import {
	mpAutoLogin
} from "@/api/connect.js";
import storage from "@/utils/storage.js";

import store from "@/store/index.js";
import config from "@/config/config";

export default function silentLogin(loginPayload = {}) {
	// console.log("调用静默登录函数");
	let provider = "weixin" // 运营商 默认是微信
	let code = "" //登录code码

	// code = 0f1MBsFa18QAjI0InZFa1RptqF1MBsF9
	return new Promise((resolve, reject) => {
		// let accessToken = storage.getAccessToken()
		// if (accessToken) {
		//   resolve(true)
		// } else {

		// }
		// #ifndef  APP-PLUS
		// 获取运营商信息
		uni.getProvider({
			service: 'oauth',
			success: async (pro) => {
				provider = pro.provider[0]
				code = await handleGetLoginCode()
				let appId
				// #ifdef MP-WEIXIN
				appId = config.appId
				// #endif
				// #ifdef MP-TOUTIAO
				appId = config.douyinAppId
				// #endif
				let payload = {
					provider: provider,
					code: code,
					appId: appId,
					AdParams: JSON.stringify({
						source: loginPayload.clickId ? 'douyinAD' : '',
						clickId: loginPayload.clickId || '',
						adid: loginPayload.adid || '',
						creativeid: loginPayload.creativeid || '',
					})
				}
				mpAutoLogin(payload).then(apiRes => {
					// 存储token
					storage.setAccessToken(apiRes.data.result.accessToken);
					storage.setRefreshToken(apiRes.data.result
						.refreshToken);
					getUserInfo().then(user => {
						let obj = {
							...user.data.result
						}
						let nickName = !user.data.result.nickName ? '我' : user.data
							.result.nickName
						obj.nickName = nickName

						storage
							.setUserInfo(obj);
						store.commit("updateUerInfo", obj)
						storage
							.setHasLogin(
								true);
						resolve(true)
						// saveUserInfo({
						//   nickName: nickName,
						// }).then(res => {
						//   storage
						//     .setUserInfo(obj);
						//   storage
						//     .setHasLogin(
						//       true);
						//   resolve(true)

						// })



					}).catch(err => {
						// console.log("失败调用 ");
						uni.showToast({
							title: "登录请求失败",
							icon: "none"
						})
						reject(false)
					})


				})



			},
			fail: (err) => {
				uni.showToast({
					title: "运营商获取失败",
					icon: 'none'
				})
				reject(false)
			}
		})
		// #endif

		// #ifdef  APP-PLUS || H5
		let payload = {
			appId: config.appId,
			code: "0f1MBsFa18QAjI0InZFa1RptqF1MBsF9"
		}
		mpAutoLogin(payload).then(apiRes => {
			// 存储token
			storage.setAccessToken(apiRes.data.result.accessToken);
			storage.setRefreshToken(apiRes.data.result
				.refreshToken);
			getUserInfo().then(user => {
				let obj = {
					...user.data.result
				}
				let nickName = !user.data.result.nickName ? '我' : user.data
					.result.nickName
				obj.nickName = nickName

				storage
					.setUserInfo(obj);
				store.commit("updateUerInfo", obj)
				storage
					.setHasLogin(
						true);
				resolve(true)
				// saveUserInfo({
				//   nickName: nickName,
				// }).then(res => {
				//   storage
				//     .setUserInfo(obj);
				//   storage
				//     .setHasLogin(
				//       true);
				//   resolve(true)

				// })



			}).catch(err => {
				// console.log("失败调用 ");
				uni.showToast({
					title: "登录请求失败",
					icon: "none"
				})
				reject(false)
			})


		})


		// #endif

	})

}


function handleGetLoginCode() {
	return new Promise((resolve, reject) => {
		uni.login({
			success: (res) => {
				if (res.errMsg === "login:ok") {
					// console.log("成功", res);
					resolve(res.code)
				}
			},
			fail: (err) => {
				uni.showToast({
					title: "登录失败",
					icon: 'none'
				})
			}

		})
	})
}

function handleGetLoginCodePlugin() {
	return new Promise((resolve, reject) => {
		uni.login({
			success: (res) => {
				if (res.errMsg === "login:ok") {
					// console.log("成功", res);
					resolve(res.code)
				}
			},
			fail: (err) => {
				uni.showToast({
					title: "登录失败",
					icon: 'none'
				})
			}

		})
	})
}
export {
	handleGetLoginCodePlugin
}